package dm71.espil.activities;

import dm71.espil.CurrentUser;
import dm71.espil.R;
import dm71.espil.REST.DataService;
import dm71.espil.models.User;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class VisProfilActivity extends Activity 
{
	TextView name;
	TextView lastName;
	TextView username;
	TextView klasse;
	TextView region;
	Button butBeFriend;
	Button butUnfriend;
	VisProfilActivity context = this;
	User userh;
	User currentUser;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.visprofilbeta);
        
        final DataService ds = new DataService();
		currentUser = CurrentUser.Instance().GetUser();
        
        UserProfile();
        
        butBeFriend = (Button)findViewById(R.id.buttonVisPRequestFriend);
        butBeFriend.setVisibility(View.GONE);
        
    	butBeFriend.setOnClickListener(new OnClickListener()
    	{
    		public void onClick(View v)
    		{    		
    			ds.BefriendUser(userh.getUserID(), currentUser.getUserID());
    			context.finish();
    		}
    	});
        
        butUnfriend = (Button)findViewById(R.id.butVisPUnFriend);
        butUnfriend.setVisibility(View.GONE);
        
    	butUnfriend.setOnClickListener(new OnClickListener()
    	{
    		public void onClick(View v)
    		{
    			ds.Unfriend(userh.UserID);
    			context.finish();
    		}
    	});
        
        boolean isFriends = ds.IsFriend(currentUser.getUserID(), userh.getUserID());
        
        if(isFriends)
        {
        	butUnfriend.setVisibility(View.VISIBLE);
        }
        else
        {
        	butBeFriend.setVisibility(View.VISIBLE);
        }
	}
	
	public void UserProfile()
	{
		name = (TextView)findViewById(R.id.textVisPName);
        lastName = (TextView)findViewById(R.id.textVisPBLastName);
        username = (TextView)findViewById(R.id.textVisPUsername);
        klasse = (TextView)findViewById(R.id.textVisPKlasse);
        region = (TextView)findViewById(R.id.textVisPRegion);
        
        try{
        	Intent startingIntent = getIntent();
            if (startingIntent != null)
            {
            	Bundle p = startingIntent.getBundleExtra("android.intent.extra.INTENT");
            	userh = (User) p.getSerializable("user");
            	name.setText(userh.getName());
            	lastName.setText(userh.getLastName());
            	username.setText(userh.getUsername());
            	klasse.setText(userh.getKlasse());
            }
        }
        catch(Exception ee)
        {
        	Log.e("ESPIL", "Some error");
        	ee.printStackTrace();
        }
	}
}
    	